<?php
// путя.рф (putya)
// 0:/var/www/putya
// petrovsa m81fai6mzB0RYxCy  akukin.com
// Для работы с БД
define ( "host" ,            "localhost" );
define ( "base" ,            "putya"    );
define ( "login" ,           "putya"  );
define ( "password" ,        "w5XtXx6UxhZxEa7q"   );

// Запросы к БД
function getmysql_query($query){
    $db = mysql_connect(host, login, password) or
        die("Could not connect: " . mysql_error());
    mysql_select_db( base, $db );
    mysql_query( "SET CHARACTER SET cp1251", $db );
    mysql_query( "SET NAMES CP1251", $db ) ;
    $result = mysql_query( $query, $db ) or die(mysql_error());

    //echo $query;
    switch (substr(trim($query),0,6))
    {
        case 'SELECT':
            while ($row = mysqli_fetch_array($result, MYSQL_ASSOC))
            { $ares[] = iconv( "WINDOWS-1251" , "UTF-8", $row); }
            mysql_free_result ($result);

            return ($ares);
        case 'INSERT':
            return (mysqli_insert_id());
        case 'UPDATE':
        case 'DELETE':
            return (mysqli_affected_rows());
    }
    return 0;
}



function getmysqli_query($query){
    /* Подключение к серверу MySQL */
    $mysqli = new mysqli(host, login, password, base);

    if (mysqli_connect_errno()) {
       printf("Подключение к серверу MySQL невозможно. Код ошибки: %s\n", mysqli_connect_error());
       exit;
    }

    $mysqli->query( "SET CHARACTER SET cp1251" );
    $mysqli->query( "SET NAMES CP1251" ) ;

    /* Посылаем запрос серверу */
    if ($result = $mysqli->query(iconv("UTF-8","WINDOWS-1251",$query)) or die(mysqli_error())) {

        /* Определяем тип запроса */
        switch (substr(trim($query),0,6))
        {
        case 'SELECT':
            /* Выбираем результаты запроса: */
            while( $row = $result->fetch_assoc() ){
                foreach ( $row as $key => $value )
                {
                    $row[$key] = iconv("WINDOWS-1251","UTF-8",$value);
                }

                $ares[] = $row;
            }

            /* Освобождаем память */
            $result->close();
            break;
        case 'INSERT':
            $ares =  $mysqli->insert_id;
            break;
        case 'UPDATE':
        case 'DELETE':
            $ares = $mysqli->affected_rows;
            break;
        }

        

    }

    /* Закрываем соединение */
    $mysqli->close();

    return $ares;
}

/*
    $mysqli->query( "SET CHARACTER SET cp1251" );
    $mysqli->query( "SET NAMES CP1251" ) ;
   // if ($result = $mysqli->query("SELECT `link` FROM `simple` WHERE `short` = '".iconv("UTF-8""WINDOWS-1251",,"аВтккЕ")."'")) {
    $query = "SELECT * FROM `simple` WHERE `short` = '".iconv("UTF-8","WINDOWS-1251","аВтккЕ")."' ";
    echo $query;

    if ($result == $mysqli->query($query)) {
        while ($row = $result->fetch_assoc())
        {
            $ares[] = iconv( "WINDOWS-1251" , "UTF-8", $row);
            echo $row;
        }

        $result->close();
    }

    $mysqli->close();

    return ($ares);
*/
?>
